<#include "/custom.include">
<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
<#assign actionExtension = "do">
package ${basepackage}.action.${moduleNameLower};

import java.util.Date;

import javacommon.constant.ResultCode;
import javacommon.jgrid.model.ResultAction;
import javacommon.jgrid.model.ResultGrid;
import javacommon.jgrid.util.FilterTranslator;
import javacommon.util.EnumDataHelper;
import javacommon.util.IDGenerate;
import cn.org.rapid_framework.page.Page;
import ${basepackage}.model.${moduleNameLower}.${className};
import ${basepackage}.service.${moduleNameLower}.${className}Manager;

public class ${className}Action extends javacommon.base.BaseAction {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "id desc";
	

	public void prepare()   {
		if ("".equals(javacommon.util.safe.SafeUtils.getString(id))) {
			${classNameLower} = new ${className}();
		} else {
			${classNameLower} = (${className})${classNameLower}Manager.getByIdMasterOrSlave(id, EnumDataHelper.DBBeans.omsmaster.getValue(), EnumDataHelper.DBBeans.omsslave.getValue());
		}
	}

	/** 列表页 */
	public String ${className}ListPage() {
		return SUCCESS;
	}

	/** 新增修改查看页 */
	public String ${className}DetailPage() {
		prepare();
		return SUCCESS;
	}

	/** 选择列表页 */
	public String ${className}SelectListPage() {
		return SUCCESS;
	}
	/** 高级搜索页 */
	public String ${className}SearchPage() {
		return SUCCESS;
	}

	/** 新增 */
	public String Add() {
		ResultAction<Long> tmpresult = new ResultAction<Long>();
		try {
			if(${classNameLower}!=null){
				String ip=javacommon.util.ip.IPHelper.getIp(this.getRequest());
				javacommon.util.SessionUtil sessionUtil=new javacommon.util.SessionUtil(this.getRequest());
				Long adminId=sessionUtil.getAdminID();
				String adminName=sessionUtil.getAdminName();
				String loggerDataSource = EnumDataHelper.DBBeans.loggermaster.getValue();
				${classNameLower}.setId(IDGenerate.getNextId());
				${classNameLower}.setCreatetime(new Date());
				${classNameLower}Manager.save(${classNameLower},EnumDataHelper.DBBeans.omsmaster.getValue(),ip,adminId,adminName,loggerDataSource);
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ok.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ok.getString());
				tmpresult.setData(${classNameLower}.getId());
			}else{
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ObjectNotExist.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ObjectNotExist.getString());
				
			}	
			
			
		} catch (Exception ex) {
			log.error(ex.getMessage());
			tmpresult.setResultCode(ResultCode.ResultCodeMapping.syserror.getValue());
			tmpresult.setMessage(ResultCode.ResultCodeMapping.syserror.getString());
		} finally {
			putJSONResult(tmpresult);
		}
		return JSON;
	}

	/** 修改 */
	public String Modify() {
		ResultAction<Long> tmpresult = new ResultAction<Long>();
		try {
			if(${classNameLower}!=null){
				String ip=javacommon.util.ip.IPHelper.getIp(this.getRequest());
				javacommon.util.SessionUtil sessionUtil=new javacommon.util.SessionUtil(this.getRequest());
				Long adminId=sessionUtil.getAdminID();
				String adminName=sessionUtil.getAdminName();
				String loggerDataSource = EnumDataHelper.DBBeans.loggermaster.getValue();
				${classNameLower}Manager.update(${classNameLower},EnumDataHelper.DBBeans.omsmaster.getValue(),ip,adminId,adminName,loggerDataSource);
				
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ok.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ok.getString());
				tmpresult.setData(${classNameLower}.getId());
			}else{
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ObjectNotExist.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ObjectNotExist.getString());
				
			}	
			
		} catch (Exception ex) {
			log.error(ex.getMessage());
			tmpresult.setResultCode(ResultCode.ResultCodeMapping.syserror.getValue());
			tmpresult.setMessage(ResultCode.ResultCodeMapping.syserror.getString());
		} finally {
			putJSONResult(tmpresult);
		}
		return JSON;
	}
	/** 删除对象 */
	public String Delete() {
		ResultAction<Integer> tmpresult = new ResultAction<Integer>();
		try {
			if(ids!=null&&!"".equals(ids)){
				String tmpcond = "ID in (" + ids + ")";
				javacommon.base.BaseQuery query = new javacommon.base.BaseQuery();
				query.setWherecond(tmpcond);
				String ip=javacommon.util.ip.IPHelper.getIp(this.getRequest());
				javacommon.util.SessionUtil sessionUtil=new javacommon.util.SessionUtil(this.getRequest());
				Long adminId=sessionUtil.getAdminID();
				String adminName=sessionUtil.getAdminName();
				String loggerDataSource = EnumDataHelper.DBBeans.loggermaster.getValue();
				int tmpcount = ${classNameLower}Manager.deleteByCond(query,EnumDataHelper.DBBeans.omsmaster.getValue(),ip,adminId,adminName,loggerDataSource);
				
				if (tmpcount > 0) {
					tmpresult.setResultCode(ResultCode.ResultCodeMapping.ok.getValue());
					tmpresult.setMessage(ResultCode.ResultCodeMapping.ok.getString());
					tmpresult.setData(tmpcount);
				} else {
					tmpresult.setResultCode(ResultCode.ResultCodeMapping.ObjectNotExist.getValue());
					tmpresult.setMessage(ResultCode.ResultCodeMapping.ObjectNotExist.getString());
					
				}
			}else{
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ObjectNotExist.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ObjectNotExist.getString());
			}	
		} catch (Exception ex) {
			log.error(ex.getMessage());
			tmpresult.setResultCode(ResultCode.ResultCodeMapping.syserror.getValue());
			tmpresult.setMessage(ResultCode.ResultCodeMapping.syserror.getString());
		} finally {
			putJSONResult(tmpresult);
		}
		return JSON;
	}

	/** 获取对象信息 */
	public String GetInfo() {
		ResultAction<${className}> tmpresult = new ResultAction<${className}>();
		try {
			if (!"".equals(javacommon.util.safe.SafeUtils.getString(id))) 
				${classNameLower} = (${className}) ${classNameLower}Manager.getByIdMasterOrSlave(id, EnumDataHelper.DBBeans.omsmaster.getValue(), EnumDataHelper.DBBeans.omsslave.getValue());
			if (${classNameLower} != null) {
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ok.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ok.getString());
				tmpresult.setData(${classNameLower});
			} else {
				tmpresult.setResultCode(ResultCode.ResultCodeMapping.ObjectNotExist.getValue());
				tmpresult.setMessage(ResultCode.ResultCodeMapping.ObjectNotExist.getString());
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			tmpresult.setResultCode(ResultCode.ResultCodeMapping.syserror.getValue());
			tmpresult.setMessage(ResultCode.ResultCodeMapping.syserror.getString());
		} finally {
			putJSONResult(tmpresult);
		}
		return JSON;
	}

	/** 获取对象列表 */
	public String GridList() {
		ResultGrid<${className}> tmpresult = new ResultGrid<${className}>();
		try {
			int pageIndex = javacommon.util.safe.SafeUtils.getInt( getRequest().getParameter("page"),1); // 取得当前页数,注意这是jqgrid自身的参数
	        int pageSize =javacommon.util.safe.SafeUtils.getInt( getRequest().getParameter("rows"),1); // 取得每页显示行数，,注意这是jqgrid自身的参数
	        String sortName = javacommon.util.safe.SafeUtils.getString( getRequest().getParameter("sidx")); //排序字段
	        String sortOrder = javacommon.util.safe.SafeUtils.getString( getRequest().getParameter("sord")); //Desc或Asc

			String tmpcond = "";
			String tmpsort = "";
			if (sortName != null && sortName != "")
				tmpsort = sortName + " " + sortOrder;
			FilterTranslator tmptrans = new FilterTranslator();
			tmpcond = tmptrans.getWhereString(where);

			javacommon.base.BaseQuery query = new javacommon.base.BaseQuery();
			query.setPageNumber(pageIndex);
			query.setPageSize(pageSize);
			query.setWherecond(tmpcond);
			query.setSortColumns(tmpsort);

			Page page = ${classNameLower}Manager.findPageByMasterOrSlave(query, EnumDataHelper.DBBeans.omsmaster.getValue(), EnumDataHelper.DBBeans.omsslave.getValue());;
			tmpresult.setRows(page.getResult());
			tmpresult.setTotal(javacommon.util.safe.SafeUtils.GetPageNum(page.getTotalCount(), pageSize));//总页数
			tmpresult.setRecords(page.getTotalCount());//总记录数
			tmpresult.setPage(pageIndex);//当前页

		} catch (Exception ex) {
			log.error(ex.getMessage());
			tmpresult.setTotal(0);
		} finally {
			putJSONResult(tmpresult);
		}
		return JSON;
	}
	
	private ${className}Manager ${classNameLower}Manager;
	private ${className} ${classNameLower};
	public Long id = null;
	// post删除多个对象
	public String ids = null;
	// post获取列表参数
	public String where = null;
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void set${className}Manager(${className}Manager value) { this.${classNameLower}Manager = value; }

	public javacommon.base.BaseEntity getModel() { return ${classNameLower}; }
	public void setId(Long value) { this.id = value; }
	public void setIds(String value) { this.ids = value; }
	public void setWhere(String value) { this.where = value; }
	
}

